package test;

import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;
import javax.swing.*;

class JRadioButtonFrame extends JFrame implements ItemListener {

    private JRadioButton r1;
    private JRadioButton r2;
    private JRadioButton r3;
    private JRadioButton r4;
    private JRadioButton r5;
    private JRadioButton r6;
    private JRadioButton r7;
    Hashtable ht = new Hashtable();
    String[] name;

    public JRadioButtonFrame(String title) {
        super(title);
        setSize(400, 500);
        //获得容器
        Container container = this.getContentPane();

        //设置布局为2行一列
        container.setLayout(new GridLayout(2, 1));

        //创建面板一,文字式问卷调查
        JPanel pane = new JPanel(new GridLayout(1, 4));

        //设置面板标题
        pane.setBorder(BorderFactory.createTitledBorder("你最喜欢的人是谁??"));

        //创建选择的选项

        r1 = new JRadioButton("老婆");
        r2 = new JRadioButton("情人");
        r3 = new JRadioButton("二奶");
        //创建默认选项
        r4 = new JRadioButton("以上都不喜欢", true);

        //创建ButtonGroup对象，不然就可以多选了，我们要的时单选
        ButtonGroup bg = new ButtonGroup();

        //在ButtonGroup中加入JRadioButton
        bg.add(r1);
        r1.addItemListener(this);
        bg.add(r2);
        r2.addItemListener(this);
        bg.add(r3);
        bg.add(r4);

        //添加到面板
        pane.add(r1);
        pane.add(r2);
        pane.add(r3);
        pane.add(r4);


        //把面板加入到容器中
        container.add(pane);

        //创建面板二,图像式JRadioButton

        JPanel pane2 = new JPanel(new GridLayout(3, 1));
        pane2.setBorder(BorderFactory.createTitledBorder("你最喜欢的明星是谁??"));
        //创建带有Icon的JRadioButton
        r5 = new JRadioButton("柳真");
        r6 = new JRadioButton("全智贤");
        r7 = new JRadioButton("宋慧乔");


        //创建ButtonGroup对象，不然就可以多选了，我们要的时单选
        ButtonGroup bg2 = new ButtonGroup();

        //在ButtonGroup中加入JRadioButton
        bg.add(r5);
        bg.add(r6);
        bg.add(r7);


        r5.addItemListener(this);
        r6.addItemListener(this);
        r7.addItemListener(this);
        //加入面板
        pane2.add(r5);
        pane2.add(r6);
        pane2.add(r7);


        //把面板加入到容器中
        container.add(pane2);

        //面板3
        JPanel pane3 = new JPanel();
        pane3.setBorder(BorderFactory.createTitledBorder("my test"));
        pane3.setLayout(new GridLayout(3, 1));

        ButtonGroup bg3 = new ButtonGroup();
        name = new String[]{"111", "222", "333"};

        ht.put(name[0], "My name is 111");
        ht.put(name[1], "My name is 222");
        ht.put(name[2], "My name is 333");
        for (int i = 0; i < 3; i++) {
            JRadioButton temp = new JRadioButton(name[i]);
            temp.addItemListener(new ItemListener() {

                public void itemStateChanged(ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        //JOptionPane.showMessageDialog(rootPane, ((JRadioButton)e.getItem()).getText().toString());
                        for (int j = 0; j < ht.size(); j++) {
                            String temp = ((JRadioButton) e.getItem()).getText();
                            if (temp.equals(name[j])) {
                                JOptionPane.showMessageDialog(
                                        rootPane,
                                        ht.get(name[j]),
                                        "show",
                                        JOptionPane.OK_OPTION);
                            }
                        }
                    }
                }
            });
            bg3.add(temp);
            pane3.add(temp);
        }

        this.add(pane3);


    }

    public void itemStateChanged(ItemEvent evt) {
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            if (evt.getSource() == r5) {
                JOptionPane.showMessageDialog(this, "好小子，有眼光，柳真是我偶像", "柳真", JOptionPane.OK_OPTION);
            }
            if (evt.getSource() == r6) {
                JOptionPane.showMessageDialog(this, "眼光一般，没有柳真好", "全知贤", JOptionPane.OK_OPTION);
            }
            if (evt.getSource() == r7) {
                JOptionPane.showMessageDialog(this, "眼光一般，没有柳真好", "宋慧乔", JOptionPane.OK_OPTION);
            } else {
                JOptionPane.showMessageDialog(this, "asdjflksjfl", "xxxx", JOptionPane.OK_OPTION);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        JRadioButtonFrame frame = new JRadioButtonFrame("JRadioButtonDemo");
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}
